package com.netty.study.handler;

import com.netty.study.dto.UserMessage;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class MessageServiceHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        ctx.writeAndFlush(new UserMessage("客户端", "加入聊天室1"));
        ctx.writeAndFlush(new UserMessage("客户端", "加入聊天室2"));
        ctx.writeAndFlush(new UserMessage("客户端", "加入聊天室3"));
        ctx.writeAndFlush(new UserMessage("客户端", "加入聊天室4"));
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        UserMessage userMessage = (UserMessage) msg;
        log.info("接收到消息：{}:{}", userMessage.getUserName(), userMessage.getMessage());
//        ctx.writeAndFlush(msg);
    }
}